package itemControllers

import (
	"fmt"
	"lazyops_go/models"
	"lazyops_go/tasks"
	"lazyops_go/tools"
)

func (c *ItemController) IndexBranch() {
	cur_page,_ := c.GetInt(":page",1)
	is_super := c.GetSession("is_super")
	cur_user_menus := c.GetSession("cur_user_menus")
	role_id_list := c.GetSession("role_id_list").([]int)
	cur_user := c.GetSession("cur_user")
	count:=models.QueryBranchCount(is_super.(bool),cur_user.(string),role_id_list)
	data_list := models.QueryBranchAll(cur_page,is_super.(bool),cur_user.(string),role_id_list)
	page:=tools.CreatePaging(cur_page,12,count)
	platfm_list := models.QueryPlatfmAll(role_id_list)

	c.Data["page"] = page
	c.Data["cur_user"] = cur_user
	c.Data["cur_user_menus"] = cur_user_menus
	c.Data["data_list"] = data_list
	c.Data["platfm_list"] = platfm_list
	c.Data["label"] = "分支管理"
	c.XSRFExpire = 7200
	c.Data["xsrf_token"] = c.XSRFToken()
	c.Layout = "base/base.html"
	c.LayoutSections = make(map[string]string)
	c.LayoutSections["Scripts"] = "items/branchjs.html"
	c.TplName = "items/branch.html"
}

func (c *ItemController) SearchBranch() {
	key := c.Ctx.Input.Param(":key")
	role_id_list := c.GetSession("role_id_list").([]int)
	data_list := models.QueryBranchSearch(key)
	platfm_list := models.QueryPlatfmAll(role_id_list)
	cur_user_menus := c.GetSession("cur_user_menus")
	cur_user := c.GetSession("cur_user")
	page:=tools.CreatePaging(1,12,len(data_list))
	c.Data["page"] = page
	c.Data["cur_user"] = cur_user
	c.Data["key"] = key
	c.Data["cur_user_menus"] = cur_user_menus
	c.Data["data_list"] = data_list
	c.Data["platfm_list"] = platfm_list
	c.Data["label"] = "分支管理"
	c.XSRFExpire = 7200
	c.Data["xsrf_token"] = c.XSRFToken()
	c.Layout = "base/base.html"
	c.LayoutSections = make(map[string]string)
	c.LayoutSections["Scripts"] = "items/branchjs.html"
	c.TplName = "items/branch.html"
}



func (c *ItemController) CreateBranch() {
	platfm_id, _ := c.GetInt("platfm_id", 0)
	branch_name := c.GetString("branch_name")
	branch_msg := c.GetString("branch_msg")
	test_url := c.GetString("test_url")
	obj := models.ItemBranch{}
	obj.PlatfmID = platfm_id
	obj.BranchName = branch_name
	obj.BranchMsg = branch_msg
	obj.TestUrl = test_url
	err := models.AddBranch(obj)
	if err != nil {
		c.Data["json"] = err
	} else {
		c.Data["json"] = "添加成功"
	}
	c.ServeJSON()
}

func (c *ItemController) GetBranchEditInfo() {
	id, _ := c.GetInt("id", 0)
	data := models.QueryBranchOne(id)
	c.Data["json"] = data
	c.ServeJSON()
}

func (c *ItemController) EditBranch() {
	id, _ := c.GetInt("id", 0)
	platfm_id, _ := c.GetInt("platfm_id", 0)
	branch_name := c.GetString("branch_name")
	branch_msg := c.GetString("branch_msg")
	test_url := c.GetString("test_url")
	obj := models.ItemBranch{}
	obj.PlatfmID = platfm_id
	obj.BranchName = branch_name
	obj.BranchMsg = branch_msg
	obj.TestUrl = test_url
	err := models.UpdateBranch(id, obj)

	if err != nil {
		c.Data["json"] = err
	} else {
		c.Data["json"] = "更新成功"
	}
	c.ServeJSON()
}

func (c *ItemController) DelBranch() {
	id, _ := c.GetInt("id", 0)
	err := models.DeleteBranch(id)
	if err != nil {
		c.Data["json"] = err
	} else {
		c.Data["json"] = "已删除"
	}
	c.ServeJSON()
}


func (c *ItemController) TestServerUp() {
	id, _ := c.GetInt("id", 0)
	obj := models.QueryBranchOne(id)
	test_url := obj.TestUrl

	taskName := "task.UpdateTestServer"
	cli := tasks.Client()
	asyncResult, err := cli.Delay(taskName, test_url)
	if err != nil {
		panic(err)
	}
	tk := models.LogTask{}
	cur_user := c.GetSession("cur_user")
	tk.UserName = cur_user.(string)
	tk.TaskName = fmt.Sprintf("内测服更新：%s---%s---%s", obj.Platfm.Prj.PrjName, obj.Platfm.PlatfmName,obj.BranchName)
	tk.Status = "PENDING"
	tk.TaskType = true
	tk.TaskId = asyncResult.TaskID
	models.AddTask(tk)
	c.Data["json"] = "任务执行中:"+asyncResult.TaskID
	c.ServeJSON()
}


